﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SpiritAlight.SceneManagement;
using SpiritAlight.Character.Unit;
using SpiritAlight.Character.Unit.EntitySpirit;
using SpiritAlight.Character.Unit.EntityEnemy;
using SpiritAlight.Stage;
using SpiritAlight.Input;

namespace SpiritAlight.Character
{
    class UnitManager
    {
        Player player;

        /// <summary>
        /// 敵リスト。
        /// </summary>
        List<AEnemy> enemyList = new List<AEnemy>();

        /// <summary>
        /// 召喚した精霊リスト。
        /// </summary>
        List<ASpirit> spiritList = new List<ASpirit>();

        /// <summary>
        /// 選択中ユニット
        /// </summary>
        Army selectUnit;

        /// <summary>
        /// このマネージャーが所属するシーン
        /// </summary>
        AScene sceneBelong;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="inScene">このオブジェクトが所属するシーン。</param>
        public UnitManager(AScene inScene)
        {
            sceneBelong = inScene;
            player = new Player(spiritList);
        }

        /// <summary>
        /// マネージャ初期化。ここでそれぞれのキャラクターの初期化を行う。
        /// </summary>
        public void Initialize()
        {
            spiritList.Add(new Spirit1(100.0f, 300.0f));
            enemyList.Add(new Goblin(300.0f, 500.0f));
            
            
            player.Initialize();

        }

        /// <summary>
        /// マネージャに必要なリソースの読み込み。
        /// </summary>
        /// <param name="Content"></param>
        public void LoadContent()
        {
            player.LoadContent();
            Goblin goblin = new Goblin(0.0f, 0.0f);
            goblin.LoadContent();
            Spirit1 spirit1 = new Spirit1(0.0f, 0.0f);
            spirit1.LoadContent();

        }


        /// <summary>
        /// マネージャの更新処理。
        /// 各インスタンスの情報を引き渡す。
        /// </summary>
        public void Update()
        {
            ///更新処理
            player.Update();
            foreach (var enemy in enemyList)
                enemy.Update();
            foreach (var spirit in spiritList)
                spirit.Update();

            BattleProc();

            if (InputState.IsMouseButton(Input.MouseButton.Left) == Input.DeviceState.Pressed)
            {
                //プレイヤーと召還獣のクリック処理
                foreach (var spirit in spiritList)
                {
                    if (spirit.IsClick(InputState.MousePosition))
                    {
                        selectUnit = spirit;
                        return;
                    }
                }
                if (player.IsClick(InputState.MousePosition))
                {
                    selectUnit = player;
                    return;
                }


                if (selectUnit != null)
                {
                    selectUnit.OnClick(InputState.MousePosition);
                }
            }

            

        }

        /// <summary>
        /// 各キャラクターの描画を行う。
        /// </summary>

        public void Draw()
        {

            var qLoadedEnemy = enemyList.Where((x)=> x.IsLoaded == true).Select((x) => x);
            var qLoadedSpirit = spiritList.Where((x) => x.IsLoaded == true).Select((x) => x);

            if (player.IsLoaded)
            {
                player.Draw();                
            }
            foreach (var enemy in qLoadedEnemy)
                enemy.Draw();
            foreach (var spirit in spiritList)
                spirit.Draw();
        }

        private void BattleProc()
        {
            List<Army> copySpiritList = new List<Army>();
            copySpiritList.AddRange(spiritList);

            foreach (var enemy in enemyList)
            {
                if (enemy.Target == null)
                    enemy.SearchOperation<Army>(copySpiritList);
                else
                    enemy.Attack();

                enemy.BattleAnimation();
            }

            foreach (var spirit in spiritList)
            {
                if (spirit.Target == null)
                    spirit.SearchOperation<AEnemy>(enemyList);
                else
                    spirit.Attack();

                spirit.BattleAnimation();
            }

            player.BattleAnimation();

            
        }
        
    }
}
